package com.neuedu.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-19 13:37
 * 描述 :
 */

@WebServlet(value = "/methods")
public class MethodsServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get请求呗执行了");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // String fav =  req.getParameter("fav");
        String[] favs =  req.getParameterValues("fav");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        for (String fav : favs) {
            System.out.println("fav = " + fav);
        }


        //
        Enumeration<String> parameterNames = req.getParameterNames();

        while(parameterNames.hasMoreElements()){
            System.out.println("请求参数的名字:  "+parameterNames.nextElement());
        }

        //key           String 参数的名字
        //value         String[] 参数的值
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<String> keys = parameterMap.keySet();
        for (String key : keys) {
            String[] values =  parameterMap.get(key);
            System.out.print("key: "+key);
            for (String value : values) {
                System.out.print(value+"\t\t");
            }
            System.out.println();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了post方法");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // String fav =  req.getParameter("fav");
        String[] favs =  req.getParameterValues("fav");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        for (String fav : favs) {
            System.out.println("fav = " + fav);
        }
    }
}
